mui.init({})
mui.plusReady(function() {
	if(plus.webview.getWebviewById('../mask/clsaddcart.html')) {
		plus.webview.getWebviewById('../mask/clsaddcart.html').close()
	}

	var swith = false
	var self = plus.webview.currentWebview();

	var resizeHeight = document.documentElement.clientHeight || document.body.clientHeight;
	$(".order_footer").css('top', resizeHeight - 44)

	self.setStyle({
		top: '0',
		bottom: '0'
	})
	console.log(JSON.stringify(self.parameter))
	Vue.use(VueLazyload, {
		preLoad: 1.3,
		error: '../../images/noimg.jpg',
		loading: '../../images/noimg.jpg'
	});
	var confirm = new Vue({
		el: '#confirm',
		data: {
			imgCommonUrl: common.imageUrl,
			shopCarts: [],
			userAddress: '',
			total: '',
			token: '',
			invoice: '',
			getOrderCoupons: [],
			useChoice: ''
		},
		methods: {
			submitOrder: function() {
				if(swith) {
					common.toast('请勿重复下单')
					return
				}
				if(!confirm.userAddress){
					common.toast("请选择地址")
					return
				}
				
				var basketStr = ''
				var remarkText = ''
				for(var i = 0; i < this.shopCarts.length; i++) {
					if(this.shopCarts[i].remarkText != '') {
						remarkText += this.shopCarts[i].shopId + ':' + this.shopCarts[i].remarkText + ';'
					}
					for(var c = 0; c < this.shopCarts[i].cartItems.length; c++) {
						for(var j = 0; j < this.shopCarts[i].cartItems[c].child.length; j++) {
							basketStr += this.shopCarts[i].cartItems[c].child[j].basketId + ','
						}
					}
				}
				if(remarkText != '') {
					remarkText = remarkText.substring(0, remarkText.length - 1)
				}

				var addId = confirm.userAddress.addrId || confirm.userAddress.addId
				swith = true
				common.jsonAjax('/submitOrder/submit', {
					basketStr: basketStr.substring(0, basketStr.length - 1),
					remarkText: remarkText,
					token: confirm.total.orderToken,
					selAddrId: addId,
					payManner: 2,
					invoiceId: confirm.invoice.invoiceId || '',
					userCouponId: confirm.useChoice.userCouponId || ''
				}, function(data) {
					if(data.code == 1) {
						console.log(data.data.subNums)
						common.open('orderdetails.html', 'orderdetails.html', data.data.subNums)
						if(plus.webview.getWebviewById('shopping.html')) {
							plus.webview.getWebviewById('shopping.html').reload()
						}
						if(plus.webview.getWebviewById('my.html')) {
							plus.webview.getWebviewById('my.html').reload()
						}
						//						setTimeout(function() {
						//							plus.webview.currentWebview().close('none')
						//						}, 400)
					} else {
						swith = false
						if(data.resultMsg) {
							common.toast(data.resultMsg)
						}
					}
				})
			},
			openAddress: function() {
				common.open('../my/address.html', 'address.html', true)
			},
			openOrderCoupons: function() {
				common.open('choicePreferences.html', 'choicePreferences.html', confirm.getOrderCoupons)
			}
		},
	})
	goBuy()

	function goBuy() {
		//立即购买
		if(JSON.stringify(self.parameter.swith)) {
			var data = self.parameter.data
			common.jsonAjax('/submitOrder/goBuy', data, function(data) {
				console.log(JSON.stringify(data))
				if(data.code == 1) {
					confirm.userAddress = data.data.userShopCartList.userAddress
					datas(data.data.userShopCartList.shopCarts)
					confirm.total = data.data.userShopCartList
					getOrderCoupons()
				} else {
					if(data.resultMsg) {
						common.toast(data.resultMsg)
					}
					setTimeout(function() {
						mui.back()
					}, 2000)
				}
			})
		} else {
			//结算
			var baskIds = self.parameter
			common.ajax('/submitOrder/balance', {
				baskIds: baskIds
			}, function(data) {
				if(data.code == 1) {
					confirm.userAddress = data.data.userShopCartList.userAddress
					datas(data.data.userShopCartList.shopCarts)
					confirm.total = data.data.userShopCartList
					getOrderCoupons()
				} else {
					if(data.resultMsg) {
						common.toast(data.resultMsg)
					}
					setTimeout(function() {
						mui.back()
					}, 2000)
				}
			})
		}
	}

	//var baskIds = '23388,23387,23385,23386'
	////		var baskIds = self.parameter
	//common.ajax('/submitOrder/balance', {
	//	baskIds: baskIds
	//}, function(data) {
	//	if(data.code == 1) {
	//		confirm.userAddress = data.data.userShopCartList.userAddress
	//		datas(data.data.userShopCartList.shopCarts)
	//		confirm.total = data.data.userShopCartList
	//	} else {
	//		if(data.resultMsg) {
	//			common.toast(data.resultMsg)
	//		}
	//	}
	//})

	function datas(shopCarts) {
		var prodId = []
		for(var i = 0; i < shopCarts.length; i++) {
			var temp = []
			for(var c = 0; c < shopCarts[i].cartItems.length; c++) {
				if(prodId.indexOf(shopCarts[i].cartItems[c].prodId) < 0) {
					var txt = JSON.parse(JSON.stringify(shopCarts[i].cartItems[c]));
					txt.child = [shopCarts[i].cartItems[c]]
					temp.push(txt)
					prodId.push(shopCarts[i].cartItems[c].prodId)
				} else {
					for(var t = 0; t < temp.length; t++) {
						if(temp[t].prodId == shopCarts[i].cartItems[c].prodId) {
							temp[t].child.push(shopCarts[i].cartItems[c])
						}
					}
				}

				shopCarts[i].remarkText = ''
			}
			shopCarts[i].cartItems = temp
		}
		confirm.shopCarts = shopCarts
	}

	window.addEventListener('update', function(evt) {
		var addlist = evt.detail.addlist;
		confirm.userAddress = addlist
	});

	window.addEventListener('invoice', function(evt) {
		var datafoor = evt.detail.datafoor;
		confirm.invoice = datafoor
		console.log(JSON.stringify(datafoor))
	});

	window.addEventListener('useChoice', function(evt) {
		var data = evt.detail.data;
		confirm.useChoice = data
		console.log(JSON.stringify(data))
	});

	document.addEventListener('goBuy', function() {
		goBuy()
	})

	//请求优惠券
	function getOrderCoupons() {
		common.ajax('/submitOrder/getOrderCoupons', {

		}, function(data) {
			if(data.code == 1) {
				confirm.getOrderCoupons = data.data.usableUserCouponList
				if(!confirm.getOrderCoupons || confirm.getOrderCoupons.length <= 0) {
					confirm.getOrderCoupons = []
				}
			} else {
				if(data.resultMsg) {
					mui.toast(data.resultMsg)
				}
			}
		}, true, 'get')
	}

})